package com.itheima.grow.model.other;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;

/**
 * rest请求返回值(JSON)
 */
public class Result {

    /**
     * JSON返回值数据体(data)
     */
    public static final String RETURN_JSON_DATA_KEY = "data";
    /**
     * JSON返回结果码
     */
    public static final String RETURN_JSON_RESULT_CODE = "resultCode";
    /**
     * JSON返回结果信息
     */
    public static final String RETURN_JSON_RESULT_MESSAGE = "resultMesg";

    private String resultCode;
    private String resultMesg;
    private Object data;

    public Result() {
    }

    private Result(String resultCode, String resultMesg, Object data) {
        this.resultCode = resultCode;
        this.resultMesg = resultMesg;
        this.data = data;
    }

    private Result(Object data) {
        this.resultCode = ResponseCode.SUCCESS_CODE;
        this.resultMesg = ResponseCode.SUCCESS_MESG;
        this.data = data;
    }

    /**
     * 返回成功的JSON串
     *
     * @param data
     * @return
     */
    public static Result success(Object data) {
        return new Result(data);
    }

    /**
     * 返回成功的JSON串
     *
     * @return
     */
    public static Result success() {
        return Result.success(null);
    }

    /**
     * 返回失败的JSON串
     *
     * @param resultCode
     * @param data
     * @return
     */
    public static Result fail(String resultCode, Object data) {
        return new Result(resultCode, ResponseCode.getMessage(resultCode), data);
    }

    /**
     * 提示信息带占位符的
     *
     * @param resultCode
     * @param resultMesgParams
     * @param data
     * @return
     */
    public static Result fail(String resultCode, String[] resultMesgParams, Object data) {
        return new Result(resultCode, ResponseCode.getMessage(resultCode, resultMesgParams), data);
    }

    /**
     * 返回失败的JSON串,数据体为null
     *
     * @param resultCode
     * @return
     */
    public static Result fail(String resultCode) {
        Object data = null;
        return Result.fail(resultCode, data);
    }

    /**
     * 返回失败的JSON串,提示信息带占位符，数据体为null
     *
     * @param resultCode
     * @param resultMesgParams
     * @return
     */
    public static Result fail(String resultCode, String[] resultMesgParams) {
        Object data = null;
        return Result.fail(resultCode, resultMesgParams, data);
    }

    /**
     * 根据结果码、结果信息、数据体生成实体
     *
     * @param resultCode
     * @param resultMesg
     * @param data
     * @return
     */
    public static Result json(String resultCode, String resultMesg, Object data) {
        return new Result(resultCode, resultMesg, data);
    }
    /**
     * 根据结果码、结果信息、数据体生成实体
     *
     * @param resultCode
     * @param data
     * @return
     */
    public static Result json(String resultCode, Object data) {
        return new Result(resultCode, ResponseCode.getMessage(resultCode), data);
    }


    /**
     * 根据结果码、结果信息、数据体的Json String生成实体
     *
     * @return
     */
    public static Result json(String jsonString) {
        JSONObject object = JSONObject.parseObject(jsonString);
        String resultCode = (String) object.get(RETURN_JSON_RESULT_CODE);
        String resultMesg = (String) object.get(RETURN_JSON_RESULT_MESSAGE);
        Object data = object.get(RETURN_JSON_DATA_KEY);
        return new Result(resultCode, resultMesg, data);
    }

    /**
     * 返回系统异常的错误信息JSON串
     *
     * @return
     */
    public static Result systemError() {
        return Result.fail(ResponseCode.SYSTEM_FAIL_CODE);
    }

    /**
     * 返回结果code
     *
     * @return
     */
    public String getResultCode() {
        return resultCode;
    }

    /**
     * 返回结果具体信息
     *
     * @return
     */
    public String getResultMesg() {
        return resultMesg;
    }

    /**
     * 返回结果的具体数据
     *
     * @return
     */
    public Object getData() {
        return data;
    }

    /**
     * 根据成功与否标识,返回不同ReturnJson结果
     *
     * @param isSuccess 成功与否
     * @param failCode  处理异常时应该返回的code
     * @return
     */
    public static Result result(boolean isSuccess, String failCode) {
        if (isSuccess) {
            return Result.success();
        }

        return Result.fail(failCode);
    }

    /**
     * @deprecated in favor of
     * {@link Result fail(String resultCode, Object data)}
     */
    @Deprecated
    public static Result fail(String resultCode, String resultMesg, Object data) {
        return new Result(resultCode, resultMesg, data);
    }

    /**
     * @deprecated in favor of
     * {@link Result fail(String resultCode)}
     */
    @Deprecated
    public static Result fail(String resultCode, String resultMesg) {
        return new Result(resultCode, resultMesg, null);
    }

    /**
     * 根据结果码判断请求是否处理成功
     *
     * @return
     */
    @JsonIgnore
    public boolean isSuccess() {
        return ResponseCode.SUCCESS_CODE.equalsIgnoreCase(resultCode);
    }

    /**
     * 获取data的json的字符串
     *
     * @return
     */
    @JsonIgnore
    public String getDataJsonString() {
        return JSON.toJSONString(this.getData());
    }

    /**
     * 系统异常
     *
     * @return
     */
    public boolean error() {
        return StringUtils.equals(this.resultCode, ResponseCode.SYSTEM_FAIL_CODE);
    }

    /**
     * data为空
     *
     * @return
     */
    @JsonIgnore
    public boolean isDataEmpty() {
        Object data = this.getData();
        if (data == null) {
            return true;
        }

        return (data instanceof String) ? StringUtils.isBlank(data.toString()) : false;
    }
}
